/**
 * 选择地区界面
 */
Ext.define("PSI.Area.DistrictSelectForm", {
			extend : "Ext.window.Window",
			config : {
				parentForm : null
			},

			initComponent : function() {
				var me = this;

				Ext.apply(me, {
							title : "选择地区",
							modal : true,
							resizable : false,
							onEsc : Ext.emptyFn,
							width : 900,
							height : 400,
							layout : "border",
							items : [{
										region : "west",
										layout : "fit",
										width : 300,
										items : [me.getProvinceGrid()]
									}, {
										region : "center",
										layout : "border",
										border : 0,
										items : [{
													region : "west",
													layout : "fit",
													width : 300,
													items : [me.getCityGrid()]
												}, {
													region : "center",
													layout : "fit",
													items : [me
															.getDistrictGrid()]
												}]
									}],
							listeners : {
								show : {
									fn : me.onWndShow,
									scope : me
								},
								close : {
									fn : me.onWndClose,
									scope : me
								}
							},
							buttons : [{
										text : "确定",
										formBind : true,
										iconCls : "PSI-button-ok",
										handler : function() {
											me.onOK();
										},
										scope : me
									}, {
										text : "取消",
										handler : function() {
											me.close();
										},
										scope : me
									}]
						});

				me.callParent(arguments);
			},

			onWndShow : function() {
				var me = this;
				me.freshProvinceGrid();
			},

			onWndClose : function() {
				var me = this;
			},

			onOK : function() {
				var me = this;

				var item = me.getProvinceGrid().getSelectionModel()
						.getSelection();
				if (item == null || item.length != 1) {
					PSI.MsgBox.showInfo("没有选择省");
					return;
				}
				var province = item[0];

				var item = me.getCityGrid().getSelectionModel().getSelection();
				if (item == null || item.length != 1) {
					PSI.MsgBox.showInfo("没有选择市");
					return;
				}

				var city = item[0];

				var item = me.getDistrictGrid().getSelectionModel()
						.getSelection();
				if (item == null || item.length != 1) {
					PSI.MsgBox.showInfo("没有选择区县");
					return;
				}

				var district = item[0];

				me.close();

				var form = me.getParentForm();
				form.__setDistrictInfo(province, city, district);
			},

			getProvinceGrid : function() {
				var me = this;
				if (me.__provinceGrid) {
					return me.__provinceGrid;
				}

				var modelName = "PSIProvince";
				Ext.define(modelName, {
							extend : "Ext.data.Model",
							fields : ["id", "name"]
						});

				me.__provinceGrid = Ext.create("Ext.grid.Panel", {
							border : 0,
							viewConfig : {
								enableTextSelection : true
							},
							columnLines : true,
							columns : [{
										xtype : "rownumberer"
									}, {
										header : "省",
										dataIndex : "name",
										menuDisabled : true,
										sortable : false,
										flex : 1
									}],
							store : Ext.create("Ext.data.Store", {
										model : modelName,
										autoLoad : false,
										data : []
									}),
							listeners : {
								select : {
									fn : me.onProvinceGridSelect,
									scope : me
								}
							}
						});

				return me.__provinceGrid;
			},

			getCityGrid : function() {
				var me = this;
				if (me.__cityGrid) {
					return me.__cityGrid;
				}

				var modelName = "PSICity";
				Ext.define(modelName, {
							extend : "Ext.data.Model",
							fields : ["id", "name", "provinceId"]
						});

				me.__cityGrid = Ext.create("Ext.grid.Panel", {
							border : 0,
							viewConfig : {
								enableTextSelection : true
							},
							columnLines : true,
							columns : [{
										xtype : "rownumberer"
									}, {
										header : "市",
										dataIndex : "name",
										menuDisabled : true,
										sortable : false,
										flex : 1
									}],
							store : Ext.create("Ext.data.Store", {
										model : modelName,
										autoLoad : false,
										data : []
									}),
							listeners : {
								select : {
									fn : me.onCityGridSelect,
									scope : me
								}
							}
						});

				return me.__cityGrid;
			},

			getDistrictGrid : function() {
				var me = this;
				if (me.__districtGrid) {
					return me.__districtGrid;
				}

				var modelName = "PSIDistrict";
				Ext.define(modelName, {
							extend : "Ext.data.Model",
							fields : ["id", "name"]
						});

				me.__districtGrid = Ext.create("Ext.grid.Panel", {
							border : 0,
							viewConfig : {
								enableTextSelection : true
							},
							columnLines : true,
							columns : [{
										xtype : "rownumberer"
									}, {
										header : "区/县",
										dataIndex : "name",
										menuDisabled : true,
										sortable : false,
										flex : 1
									}],
							store : Ext.create("Ext.data.Store", {
										model : modelName,
										autoLoad : false,
										data : []
									}),
							listeners : {}
						});

				return me.__districtGrid;
			},

			freshProvinceGrid : function() {
				var me = this;

				me.getCityGrid().getStore().removeAll();
				me.getDistrictGrid().getStore().removeAll();

				var grid = me.getProvinceGrid();
				var el = grid.getEl() || Ext.getBody();
				el.mask(PSI.Const.LOADING);
				Ext.Ajax.request({
							url : PSI.Const.BASE_URL
									+ "Home/Customer/provinceList",
							method : "POST",
							callback : function(options, success, response) {
								var store = grid.getStore();

								store.removeAll();

								if (success) {
									var data = Ext.JSON
											.decode(response.responseText);
									store.add(data);
								}

								el.unmask();
							}
						});

			},

			freshCityGrid : function() {
				var me = this;

				var item = me.getProvinceGrid().getSelectionModel()
						.getSelection();
				if (item == null || item.length != 1) {
					return;
				}

				var province = item[0];

				var grid = me.getCityGrid();
				var el = grid.getEl() || Ext.getBody();
				el.mask(PSI.Const.LOADING);
				Ext.Ajax.request({
							url : PSI.Const.BASE_URL + "Home/Customer/cityList",
							params : {
								provinceId : province.get("id")
							},
							method : "POST",
							callback : function(options, success, response) {
								var store = grid.getStore();

								store.removeAll();

								if (success) {
									var data = Ext.JSON
											.decode(response.responseText);
									store.add(data);
								}

								el.unmask();
							}
						});

			},

			freshDistrictGrid : function() {
				var me = this;

				var item = me.getCityGrid().getSelectionModel().getSelection();
				if (item == null || item.length != 1) {
					return;
				}

				var city = item[0];

				var grid = me.getDistrictGrid();
				var el = grid.getEl() || Ext.getBody();
				el.mask(PSI.Const.LOADING);
				Ext.Ajax.request({
							url : PSI.Const.BASE_URL
									+ "Home/Customer/districtList",
							params : {
								cityId : city.get("id")
							},
							method : "POST",
							callback : function(options, success, response) {
								var store = grid.getStore();

								store.removeAll();

								if (success) {
									var data = Ext.JSON
											.decode(response.responseText);
									store.add(data);
								}

								el.unmask();
							}
						});
			},

			onProvinceGridSelect : function() {
				var me = this;
				me.getCityGrid().getStore().removeAll();
				me.getDistrictGrid().getStore().removeAll();

				me.freshCityGrid();
			},

			onCityGridSelect : function() {
				var me = this;
				me.getDistrictGrid().getStore().removeAll();
				me.freshDistrictGrid();
			}
		});